﻿package mortal.game.view.common.guide.stepOperater
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.greensock.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.mvc.core.*;

    public class AutoGuideFlyEffectSteper extends Object implements IAutoGuideSteper
    {
        private var _start:SWFPlayer;
        private var ex:int;
        private var ey:int;
        private var sx:int;
        private var sy:int;
        private var _data:AutoGuideData;
        private var _playing:Boolean = false;
        public var preEndCallback:Function;
        public var endCallback:Function;
        private static var _loading:Boolean = false;
        private static var _loaded:Boolean = false;

        public function AutoGuideFlyEffectSteper()
        {
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            var _loc_3:Array = null;
            var _loc_4:DisplayObjectContainer = null;
            var _loc_5:String = null;
            var _loc_6:DisplayObject = null;
            var _loc_7:Point = null;
            this._data = param1;
            if (param1.guideTypeValue.indexOf(",") > 0)
            {
                _loc_3 = param1.guideTypeValue.split(",");
                this.ex = int(_loc_3[0]);
                this.ey = int(_loc_3[1]);
            }
            else if (param1.guideTypeValue.indexOf(";") > 0)
            {
                _loc_3 = param1.guideTypeValue.split(";");
                _loc_4 = LayerManager[_loc_3[0]];
                _loc_5 = _loc_3[1];
                _loc_6 = DisplayUtil.getChildByPropety(_loc_4, "name", _loc_5);
                if (_loc_6 != null)
                {
                    _loc_7 = _loc_6.localToGlobal(new Point(0, 0));
                    this.ex = _loc_7.x + 28;
                    this.ey = _loc_7.y + 27;
                }
            }
            this.sx = Global.stage.stageWidth / 2;
            this.sy = Global.stage.stageHeight / 2;
            if (!_loaded && !_loading)
            {
                this.loadEffect();
            }
            else
            {
                this.checkAndStart();
            }
            return;
        }// end function

        public function stop(param1:AutoGuideData = null) : void
        {
            this._playing = false;
            if (this.endCallback != null)
            {
                this.endCallback.apply();
                this.endCallback = null;
            }
            if (this._start != null)
            {
                this._start.dispose(true);
                this._start = null;
            }
            if (this._data != null && this._data.mainId >= 0 && this._data.stepId > 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, this._data));
            }
            return;
        }// end function

        private function loadEffect() : void
        {
            _loading = true;
            LoaderManager.instance.load("guideFly_Start.swf", this.onStartEffLoaded);
            return;
        }// end function

        private function onStartEffLoaded(param1:SWFInfo) : void
        {
            LoaderManager.instance.load("guideFly_End.swf", this.onEndEffLoaded);
            return;
        }// end function

        private function onEndEffLoaded(param1:SWFInfo) : void
        {
            _loading = false;
            _loaded = true;
            this.checkAndStart();
            return;
        }// end function

        private function checkAndStart() : void
        {
            if (this._playing)
            {
                return;
            }
            this._playing = true;
            this._start = UICompomentPool.getUICompoment(SWFPlayer);
            var _loc_1:int = 0;
            this._start.y = 0;
            this._start.x = _loc_1;
            this._start.move(this.sx, this.sy);
            this._start.load("guideFly_Start.swf", ModelType.NormalSwf, null);
            this._start.framesPlayerCompleteHandler = this.startEndHandler;
            LayerManager.highestLayer.addChild(this._start);
            return;
        }// end function

        private function startEndHandler(param1:SWFPlayer) : void
        {
            this._start.framesPlayerCompleteHandler = null;
            this._start.gotoAndStop(this._start.totalFrames);
            this._start.stop();
            TweenMax.to(this._start, 0.5, {x:this.ex - 60, y:this.ey - 70, onComplete:this.startTweenComplete});
            return;
        }// end function

        private function startTweenComplete() : void
        {
            if (this.preEndCallback != null)
            {
                this.preEndCallback.apply();
                this.preEndCallback = null;
            }
            if (this._start == null)
            {
                return;
            }
            this._start.dispose(true);
            this._start = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
            var _loc_1:int = 0;
            this._start.y = 0;
            this._start.x = _loc_1;
            this._start.move(this.ex, this.ey);
            this._start.load("guideFly_End.swf", ModelType.NormalSwf, null);
            this._start.framesPlayerCompleteHandler = this.endEndHandler;
            LayerManager.highestLayer.addChild(this._start);
            return;
        }// end function

        private function endEndHandler(param1:SWFPlayer) : void
        {
            this._start.framesPlayerCompleteHandler = null;
            this.stop();
            return;
        }// end function

    }
}
